<?php

namespace App\Model;

use App\User;
use Illuminate\Database\Eloquent\Model;

class UserLikeModel extends Model
{
    protected $table ='user_like';
    public $timestamps = false;
    protected $guarded = [];



    public function SaveCardLike($uuid, $cid,$head_image,$uid,$common_id)
    {
        $res =  UserLikeModel::create([
            'uid' => $uuid,
            'cid' => $cid,
            'head_img' =>$head_image,
            'toid' => $uid,
            'common_id' =>$common_id,
            'create_at' => date('Y-m-d H:i:s')
        ]);


        if($res){//成功！

            if(!empty($cid)){
                $card = CardModel::find($cid);
                $tCid = $card -> uid;
                $card ->like_num = $card -> like_num + 1;
                $card ->save();

                $user = UserModel::find($uuid);
                $user -> like_num = $user ->like_num + 1;
                $user -> save();

                $buser = UserModel::find($tCid);
                $buser -> m_like_num = $buser -> m_like_num + 1;
                $buser -> save();
            }

                if(!empty($common_id)){
                    $common = CommentModel::find($common_id);
                    $common ->comment_like = $common -> comment_like  + 1;
                    $common ->save();
                }


            return ['code' => true,'data' => 1];
        }else{
            return ['code' => false,'data' => 0];
        }
    }

    public function DeletedCardLike($uuid, $cid,$common_id)
    {
        if(!empty($common_id)){
            $res =  UserLikeModel::where([
                'uid' => $uuid,
                'common_id' => $common_id,
            ]) ->delete();
        }else{
            $res =  UserLikeModel::where([
                'uid' => $uuid,
                'cid' => $cid,
            ]) ->delete();
        }


        if($res){//成功！

            if(!empty($cid)){
                $card = CardModel::find($cid);
                $tCid = $card -> uid;
                if($card ->like_num > 0 ){
                    $card ->like_num = $card -> like_num - 1;
                    $card ->save();
                }

                $user = UserModel::find($uuid);
                if($user -> like_num > 0){
                    $user -> like_num = $user ->like_num - 1;
                    $user -> save();
                }

                $buser = UserModel::find($tCid);
                if($buser ->m_like_num > 0){
                    $buser -> m_like_num = $buser -> m_like_num - 1;
                    $buser -> save();
                }
            }

            if(!empty($common_id)){
                $common = CommentModel::find($common_id);
                if( $common ->comment_like > 0){
                    $common ->comment_like = $common -> comment_like  - 1;
                    $common ->save();
                }
            }

            return ['code' => true,'data' => 1];
        }else{
            return ['code' => false,'data' => 0];
        }

    }

    public function CardLikeNum($cid)
    {

        $res =  UserLikeModel::where(['cid' =>$cid]) ->count();
        if($res){//成功！
            return ['code' => true,'data' => $res];
        }else{
            return ['code' => false,'data' => '查询数据失败！'];
        }
    }

    public function CardLikeUserImage($cid)
    {

       $res = UserLikeModel::where(['cid' => $cid]) ->orderBy('create_at','desc') -> limit(3) ->  get();

        $data = $this ->ret($res);

        if(count($res)){//成功！
            return ['code' => true,'data' => $data];
        }else{
            return ['code' => false,'data' => '数据为空！'];
        }
    }

    public function ret($res){
        $data = [];
        foreach ($res as $key => $value){
            $data[$key]['img'] = $value ->head_img;
        }
        return $data;
    }

    public function SaveCollect($uuid, $cid)
    {
        $res = FavoriteModel::create([
            'uid' => $uuid,
            'cid' => $cid,
            'created_at' => date('Y-m-d H:i:s')
        ]);


        $user = UserModel::find($uuid);
        $user['fav_num'] = $user ->fav_num +1;
        $user ->save();

        $card = CardModel::find($cid);
        $card['fav_num'] = $card ->fav_num +1;
        $card -> save();


        if($res){//成功！
            return ['code' => true,'data' => 1];
        }else{
            return ['code' => false,'data' =>0];
        }
    }

    public function DeletedCollect($uuid, $cid)
    {
        $res = FavoriteModel::where(['uid' =>$uuid,'cid' =>$cid]) ->delete();

        if($res){//成功！

            $user = UserModel::find($uuid);
            if($user ->fav_num > 0){
                $user['fav_num'] = $user ->fav_num  - 1;
                $user ->save();
            }

            $card = CardModel::find($cid);
            if($card ->fav_num > 0){
                $card['fav_num'] = $card ->fav_num - 1;
                $card -> save();
            }

            return ['code' => true,'data' => 1];
        }else{
            return ['code' => false,'data' => 0];
        }
    }

    public function getLikeNum($uuid)
    {

        $res =  UserLikeModel::where(['toid' => $uuid]) ->count();

        if($res){//成功！
            return ['code' => true,'data' => $res];
        }else{
            return ['code' => false,'data' => 0];
        }

    }

    public function getLisk($uuid, $toid)
    {
        if(is_null($toid) || $toid == 'undefined'){
            $result = [];
            $data = UserLikeModel::where(['uid' => $uuid]) ->get(); //得到所有cid
            foreach ($data as $key => $value){
                $result[$key] = $value -> cid;
            }

            if(!is_null($data)){

                $res = CardModel::
                whereIn('id',$result) ->
                withCount(['comment']) ->
                withCount(['cardLike']) ->
                with([
                    'comment' => function($query){
                        $query -> select(['id','cid','img','content','comment_like'])->orderBy('comment_like','desc') -> first();
                    },
                    'image' =>function($query)  {
                        $query ->select(['id','card_id','img'])  ->get();
                    },
                    'cardLike' => function($query) {
                        $query -> select(['id','cid']) ->first() ;
                    }

                ]) -> where(['is_delete' => 0])    -> paginate(10);
                if($res){
                    return ['code' => true,'data' =>  $res ->makeHidden(['updated_at','fav_num','status','is_delete','delete_at','fid',]) -> shuffle()];
                }else{
                    return ['code' => false,'data' => '数据为空'];
                }

            }
            return ['code' => false,'data' => '数据为空'];

        }else{

            $result = [];
            $data = UserLikeModel::where(['uid' => $toid]) ->get(); //得到所有cid
            foreach ($data as $key => $value){
                $result[$key] = $value -> cid;
            }

            if(!is_null($data)){

                $res = CardModel::
                whereIn('id',$result) ->
                withCount(['comment']) ->
                withCount(['cardLike']) ->
                with([
                    'comment' => function($query){
                        $query -> select(['id','cid','img','content','comment_like'])->orderBy('comment_like','desc') -> first();
                    },
                    'image' =>function($query)  {
                        $query ->select(['id','card_id','img'])  ->get();
                    },
                    'cardLike' => function($query) {
                        $query -> select(['id','cid']) ->first() ;
                    }

                ]) -> where(['is_delete' => 0])    -> paginate(10);
                if($res){
                    return ['code' => true,'data' =>  $res ->makeHidden(['updated_at','fav_num','status','is_delete','delete_at','fid',]) -> shuffle()];
                }else{
                    return ['code' => false,'data' => '数据为空'];
                }

            }
            return ['code' => false,'data' => '数据为空'];

        }

    }


}
